package com.zbkj.admin.controller;

import com.zbkj.admin.request.OrderPayRequest;
import com.zbkj.admin.request.WechatPaymentQueryRequest;
import com.zbkj.admin.response.CashierInfoResponse;
import com.zbkj.admin.response.OrderPayResultResponse;
import com.zbkj.admin.response.PayConfigResponse;
import com.zbkj.admin.result.CommonResult;
import com.zbkj.admin.service.OrderService;
import com.zbkj.admin.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.WebAsyncTask;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 支付控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/front/pay")
@Tag(name = "支付管理")
public class PayFrontController {


    @Autowired
    private PayService payService;
    @Autowired
    private OrderService orderService;

    @Operation(summary = "获取支付配置")
    @RequestMapping(value = "/get/config", method = RequestMethod.GET)
    public CommonResult<PayConfigResponse> getPayConfig() {
        return CommonResult.success(payService.getPayConfig());
    }

    @Operation(summary = "订单支付")
    @RequestMapping(value = "/payment", method = RequestMethod.POST)
    public WebAsyncTask<CommonResult<OrderPayResultResponse>> payment(@RequestBody @Validated OrderPayRequest orderPayRequest) {
        return new WebAsyncTask<>(3000L, () -> CommonResult.success(payService.payment(orderPayRequest)));
    }

    @Operation(summary = "查询订单微信支付结果")
    @RequestMapping(value = "/query/wechat/pay/result/{orderNo}", method = RequestMethod.GET)
    public WebAsyncTask<CommonResult<Boolean>> queryPayResult(@PathVariable(value = "orderNo") String orderNo) {
        return new WebAsyncTask<>(3000L, () -> CommonResult.success(payService.queryWechatPayResult(orderNo)));
    }

    @Operation(summary = "查询订单支付宝支付结果")
    @RequestMapping(value = "/query/ali/pay/result/{orderNo}", method = RequestMethod.GET)
    public CommonResult<Boolean> queryAliPayResult(@PathVariable(value = "orderNo") String orderNo) {
        return CommonResult.success(payService.queryAliPayResult(orderNo));
    }

    @Operation(summary = "获取收银台信息")
    @RequestMapping(value = "/get/cashier/{orderNo}", method = RequestMethod.GET)
    public CommonResult<CashierInfoResponse> getCashierIno(@PathVariable(value = "orderNo") String orderNo) {
        return CommonResult.success(payService.getCashierIno(orderNo));
    }

    @Operation(summary = "查询微信支付结果")
    @RequestMapping(value = "/query/wechat/payment/result", method = RequestMethod.GET)
    public CommonResult<Boolean> queryWechatPaymentResult(@Validated WechatPaymentQueryRequest request) {
        return CommonResult.success(payService.queryWechatPaymentResult(request));
    }
}
